package com.xyy.yueshijia.topic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.google.gson.Gson;
import com.xyy.yueshijia.R;
import com.xyy.yueshijia.myutil.HttpResponse;
import com.xyy.yueshijia.myutil.MyHttpUtils;
import com.xyy.yueshijia.myutil.NetConfig;
import com.xyy.yueshijia.topic.topicbean.HoudongBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/2/13.
 */

public class HoudongFragment extends Fragment {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshView)
    SwipeRefreshLayout swipeRefreshView;
   /* @BindView(R.id.emptyView)
    android.widget.ViewStub emptyView;*/


    private ArrayList<HoudongBean> datas;
    private HuodongAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.houdong_fragment, container, false);
        datas=new ArrayList<>();
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        swipeRefreshView.setColorSchemeColors(getResources().getColor(R.color.red));
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new HuodongAdapter(getContext(), datas);
        recyclerView.setAdapter(adapter);
        swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData();
            }
        });


        swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData();
                swipeRefreshView.setRefreshing(false);
            }
        });

        //自动加载
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //在布局加载完成之后，才能执行，不然看不到效果
                swipeRefreshView.setRefreshing(true);
                loadData();
               swipeRefreshView.setRefreshing(false);
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });


    }


    //http://interface.yueshichina.com/
    // ?act=app&op=special_programa&special_type=i&key=f5492f263ab68e5b9c85b2a26c6f820e
    // &token=749a036dc06ae8b3a120848995a9f306&client=android


    //http://interface.yueshichina.com/
    // ?act=app&op=special_programa&v=2.1.2&req_time=1486533706420&special_type=6
    // &key=&channel=yingyongbao&token=749a036dc06ae8b3a120848995a9f306&client=android
    private void loadData() {

        HashMap<String, String> params = new HashMap<>();
        params.put("act", "app");
        params.put("op", "special_programa");
        params.put("v", "2.1.2");
        params.put("req_time", "1486533706420");
        params.put("special_type", "6");
        params.put("key", "");
        params.put("channel", "yingyongbao");
        params.put("token", "749a036dc06ae8b3a120848995a9f306");
        params.put("client", "android");

        MyHttpUtils.getInstance().get(NetConfig.BASE_URL, "", params, new HttpResponse() {
            @Override
            public void onResponse(String data) {
                Log.e("====活动json", "====活动json" + data);


                try {
                    JSONObject jsonObject =new JSONObject(data);

                    JSONObject jsonObject2 = jsonObject.optJSONObject("datas");
                 JSONArray jsonArray = jsonObject2.optJSONArray("virtual");
                  Log.e("===jsonArray","===jsonArray===="+jsonArray);

                 Gson gson=new Gson();
                 for(int i=0;i<jsonArray.length();i++) {
                   //  String string = jsonArray.getString(i);
                      String string = jsonArray.get(i).toString();
                     Log.e("===string", "===string====" + string);
                     Log.i("llllllllllllllll", "==============" + string);
                     datas.add(gson.fromJson(string, HoudongBean.class));

                 }


                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(String msg) {

            }
        });


    }


}
